package com.ynet.middleground.user.model;

import cfca.ra.common.vo.request.CertServiceRequestVO;
import cfca.ra.common.vo.request.QueryRequestVO;
import cfca.ra.common.vo.response.CertServiceResponseVO;
import cfca.ra.common.vo.response.QueryResponseListVO;
import cfca.ra.common.vo.response.QueryResponseVO;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynet.core.exception.BusinessException;
import com.ynet.middleground.user.bean.*;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.common.SystemConstant;
import com.ynet.middleground.user.dao.CertificateLogMapper;
import com.ynet.middleground.user.dao.CertificateManageMapper;
import com.ynet.middleground.user.dao.EnterpriseBasicInfoMapper;
import com.ynet.middleground.user.dao.EnterpriseUserInfoMapper;
import com.ynet.middleground.user.dao.NaturalPersonBaseInformationMapper;
import com.ynet.middleground.user.dao.OrganizationalStructureMapper;
import com.ynet.middleground.user.dao.SysDataDicMapper;
import com.ynet.middleground.user.dao.UserBaseInformationMapper;
import com.ynet.middleground.user.dto.*;
import com.ynet.middleground.user.entity.CertificateLog;
import com.ynet.middleground.user.entity.CertificateManage;
import com.ynet.middleground.user.entity.EnterpriseBasicInfo;
import com.ynet.middleground.user.entity.EsignLog;
import com.ynet.middleground.user.entity.OrganizationalStructure;
import com.ynet.middleground.user.entity.UserBaseInformation;
import com.ynet.middleground.user.external.cfca.CfcaCertService;
import com.ynet.middleground.user.external.cfca.CfcaConfig;
import org.apache.commons.lang.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 证书管理 服务实现类
 * </p>
 *
 * @author Wangxz
 * @since 2019-11-05
 */
@Component
public class CertificateManageModel extends ServiceImpl<CertificateManageMapper, CertificateManage> {
    @Autowired
    private Mapper mapper;

    // 注入用户信息主表
    @Autowired
    private UserBaseInformationMapper userBaseInformationMapper;
    
    @Autowired
    private EnterpriseBasicInfoMapper enterpriseBasicInfoMapper;
    
    @Autowired
    private EnterpriseUserInfoMapper enterpriseUserInfoMapper;
    
    @Autowired
    private NaturalPersonBaseInformationMapper naturalPersonBaseInformationMapper;

    //注入数据字典
    @Autowired
    private SysDataDicMapper sysDataDicMapper;

    // 注入CFCA相关服务类
    @Autowired
    private CfcaCertService cfcaCertService;

    // 注入CFCA配置信息类
    @Autowired
    private CfcaConfig cfcaConfig;
    
    @Autowired
    OrganizationalStructureMapper organizationalStructureMapper;
    
    @Autowired
    CertificateLogMapper certificateLogMapper;

    // 定义用户信息主表的QueryWrapper对象
    private QueryWrapper<UserBaseInformation> userBaseInformationQueryWrapper = null;

    /**
     * CFCA证书生成并下载
     *
     * @author Wangxz
     * @since 2019-11-05
     * @param generateCertReq
     * @return generateCertDto
     */
    @Transactional(rollbackFor = Exception.class)
    public GenerateCertDto generateCert(GenerateCertReq generateCertReq) throws Exception {
        CertServiceRequestVO certServiceRequestVO = mapper.map(cfcaConfig, CertServiceRequestVO.class);
        if(generateCertReq.getUserId()!=null) {
            // 获取用户信息
            UserBaseInformation userBaseInformation = queryUserBaseInformation(generateCertReq.getUserId());
//        LambdaQueryWrapper<EnterpriseBasicInfo> queryWrapperEBI = new LambdaQueryWrapper<EnterpriseBasicInfo>();
//        queryWrapperEBI.eq(EnterpriseBasicInfo::getUserId, generateCertReq.getUserId());
            // 验证用户是否存在，港内员工除外
            if (null == userBaseInformation) {
                throw new BusinessException(ErrorMessageConstant.ECUC0002_MESSAGE, ErrorMessageConstant.ECUC0002_CODE);
            }
            if(StringUtils.isNotBlank(generateCertReq.getUkeyId())){
                QueryWrapper<CertificateManage> uKey = new QueryWrapper<>();
                // 验证uKey是否被使用
                uKey.eq("ukey_id", generateCertReq.getUkeyId());
                if (this.count(uKey) != 0) {
                    throw new BusinessException(ErrorMessageConstant.ECUC0032_MESSAGE, ErrorMessageConstant.ECUC0032_CODE);
                }
                // 验证是否已申请该uKey
                if (null != queryUKeyInfo(generateCertReq.getUserId(), generateCertReq.getUkeyId())) {
                    throw new BusinessException("用户已申请该uKey，请勿重复申请", ErrorMessageConstant.ECUC0028_CODE);
                }
            }
            if (SystemConstant.USER_TYPE_0.equals(userBaseInformation.getUserType())) {
                LambdaQueryWrapper<EnterpriseBasicInfo> queryWrapperEBI = new LambdaQueryWrapper<EnterpriseBasicInfo>();
                queryWrapperEBI.eq(EnterpriseBasicInfo::getUserId, generateCertReq.getUserId());
                EnterpriseBasicInfo enterpriseBasicInfo = enterpriseBasicInfoMapper.selectOne(queryWrapperEBI);
                if (null == enterpriseBasicInfo) {
                    throw new BusinessException("该用户不是企业用户", ErrorMessageConstant.ECUC0002_CODE);
                }
                certServiceRequestVO.setUserName(enterpriseBasicInfo.getCompanyName());
                // 转换证件类型码
                certServiceRequestVO.setIdentType(queryPapersType(enterpriseBasicInfo.getPapersType()));
                certServiceRequestVO.setIdentNo(enterpriseBasicInfo.getPapersNo());
            } else if (SystemConstant.USER_TYPE_4.equals(userBaseInformation.getUserType())) {
                LambdaQueryWrapper<OrganizationalStructure> queryWrapperOS = new LambdaQueryWrapper<OrganizationalStructure>();
                queryWrapperOS.eq(OrganizationalStructure::getOrganizationalId, generateCertReq.getUserId());
                OrganizationalStructure organizationalStructure = organizationalStructureMapper.selectOne(queryWrapperOS);
                certServiceRequestVO.setUserName(organizationalStructure.getOrganizationalName());
                // 转换证件类型码
                certServiceRequestVO.setIdentType(queryPapersType(organizationalStructure.getPapersType()));
                certServiceRequestVO.setIdentNo(organizationalStructure.getPapersNo());
            }
        }else{
            certServiceRequestVO.setUserName(generateCertReq.getUserName());
            // 转换证件类型码
            certServiceRequestVO.setIdentType(queryPapersType(generateCertReq.getPapersType()));
            certServiceRequestVO.setIdentNo(generateCertReq.getPapersNo());
        }
        // 创建CFCA请求对象
        certServiceRequestVO.setTxCode(SystemConstant.TX_CODE_1101);
        certServiceRequestVO.setCustomerType(generateCertReq.getCustomerType());
        certServiceRequestVO.setP10(generateCertReq.getP10());
        certServiceRequestVO.setDuration(generateCertReq.getDuration());
        // 申请证书并下载
        CertServiceResponseVO certServiceResponseVO = cfcaCertService.certServiceRequest(certServiceRequestVO);
        // 将证书信息存库
        CertificateManage certificateManage = mapper.map(certServiceResponseVO, CertificateManage.class);
        certificateManage.setUserId(generateCertReq.getUserId());
        certificateManage.setUkeyId(generateCertReq.getUkeyId());
        // 证书状态，已激活
        certificateManage.setChannel(generateCertReq.getChannel());
        certificateManage.setUserName(generateCertReq.getUserName());
        certificateManage.setPapersNo(generateCertReq.getPapersNo());
        certificateManage.setPapersType(generateCertReq.getPapersType());
        certificateManage.setCreateBy(generateCertReq.getOperationUserId());
        certificateManage.setCertStatus(SystemConstant.CERT_STATUS_4);
        certificateManage.setGmtCreate(LocalDateTime.now());
        certificateManage.setGmtModified(LocalDateTime.now());
        certificateManage.setModifiedBy(generateCertReq.getOperationUserId());
        boolean save = this.save(certificateManage);
        if (save) {
            return mapper.map(certServiceResponseVO, GenerateCertDto.class);
        } else {
            throw new BusinessException("证书下载失败！", ErrorMessageConstant.ECUC0028_CODE);
        }
    }

    /**
     * CFCA证书更新并下载
     *
     * @param updateDownloadCertReq
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public UpdateDownloadCertDto updateAndDownloadCert(UpdateDownloadCertReq updateDownloadCertReq) throws Exception {
        // 验证用户是否存在
        /*if (verifyUserInformation(updateDownloadCertReq.getUserId())) {
            throw new BusinessException("用户不存在，请重新输入！", "BE00001");
        }*/
        // 创建CFCA请求对象
        CertServiceRequestVO certServiceRequestVO = mapper.map(cfcaConfig, CertServiceRequestVO.class);
        certServiceRequestVO.setTxCode(SystemConstant.TX_CODE_1201);
        LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
        CertificateManage certificateInfo = new CertificateManage();
        if(updateDownloadCertReq.getUserId()!=null){
            if(StringUtils.isNotBlank(updateDownloadCertReq.getUkeyId())) {
                // 根据id，key号查询证书信息
                certificateInfo =
                        queryUKeyInfo(updateDownloadCertReq.getUserId(), updateDownloadCertReq.getUkeyId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书更新失败,uKey或用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }else{
                // 根据id号查询证书信息
                certificateInfo =
                        queryUKeyInfoByUserId(updateDownloadCertReq.getUserId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书更新失败,用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }
            certServiceRequestVO.setDn(certificateInfo.getDn());
            queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else if(updateDownloadCertReq.getUserId()==null&&StringUtils.isNotBlank(updateDownloadCertReq.getUkeyId())){
            // 根据key号查询证书信息
            certificateInfo =
                    queryUKeyInfoByUKeyId(updateDownloadCertReq.getUkeyId());
            if (null == certificateInfo) {
                throw new BusinessException("证书更新失败,uKey信息不存在！", ErrorMessageConstant.ECUC0028_CODE);
            }
            certServiceRequestVO.setDn(certificateInfo.getDn());
            queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else{
            certServiceRequestVO.setDn(updateDownloadCertReq.getDn());
            queryWrapper.eq(CertificateManage::getDn, updateDownloadCertReq.getDn());
        }
        certServiceRequestVO.setP10(updateDownloadCertReq.getP10());
        certServiceRequestVO.setDuration(updateDownloadCertReq.getDuration());
        // 更新证书并下载
        CertServiceResponseVO certServiceResponseVO = cfcaCertService.certServiceRequest(certServiceRequestVO);
        // 更新证书数据
//        certificateInfo.setSerialNo(certServiceResponseVO.getSerialNo());
//        certificateInfo.setStartTime(certServiceResponseVO.getStartTime());
//        certificateInfo.setEndTime(certServiceResponseVO.getEndTime());
//        certificateInfo.setSignatureCert(certServiceResponseVO.getSignatureCert());
//        certificateInfo.setModifiedBy(updateDownloadCertReq.getOperationUserId());
//        certificateInfo.setGmtModified(LocalDateTime.now());
//        boolean updateById = this.updateById(certificateInfo);
//        if (updateById) {
//            // 返回结果
//            return mapper.map(certServiceResponseVO, UpdateDownloadCertDto.class);
//        } else {
//            throw new BusinessException("证书更新失败", ErrorMessageConstant.ECUC0028_CODE);
//        }
        
        boolean update = true;
        List<CertificateManage> lists = this.list(queryWrapper);
        for(int i=0;i<lists.size();i++) {
            lists.get(i).setSerialNo(certServiceResponseVO.getSerialNo());
            lists.get(i).setStartTime(certServiceResponseVO.getStartTime());
            lists.get(i).setEndTime(certServiceResponseVO.getEndTime());
            lists.get(i).setSignatureCert(certServiceResponseVO.getSignatureCert());
            lists.get(i).setModifiedBy(updateDownloadCertReq.getOperationUserId());
            lists.get(i).setGmtModified(LocalDateTime.now());
            update = this.updateById(lists.get(i));
            if (!update) {
                throw new BusinessException("证书更新失败！", ErrorMessageConstant.ECUC0028_CODE);
            }
        }
        return mapper.map(certServiceResponseVO, UpdateDownloadCertDto.class);
    }

    /**
     * 证书冻结
     *
     * @param certFreezeReq
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void freezeCert(CertFreezeReq certFreezeReq) throws Exception {
        // 验证用户是否存在
        /*if (verifyUserInformation(certFreezeReq.getUserId())) {
            throw new BusinessException("用户不存在，请重新输入！", "BE00001");
        }*/
        // 创建CFCA请求参数对象
        LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
        CertServiceRequestVO serviceRequestVO = mapper.map(cfcaConfig, CertServiceRequestVO.class);
        serviceRequestVO.setTxCode(SystemConstant.TX_CODE_2501);
        // 根据key号、id 查询证书DN
        CertificateManage certificateInfo = new CertificateManage();
        if(certFreezeReq.getUserId()!=null) {
            if(StringUtils.isNotBlank(certFreezeReq.getUkeyId())){
                certificateInfo = queryUKeyInfo(certFreezeReq.getUserId(), certFreezeReq.getUkeyId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书冻结失败，uKey或用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }else{
                certificateInfo = queryUKeyInfoByUserId(certFreezeReq.getUserId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书冻结失败，用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
            queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else if(certFreezeReq.getUserId()==null&&StringUtils.isNotBlank(certFreezeReq.getUkeyId())){
            // 根据key号查询证书信息
            certificateInfo =
                    queryUKeyInfoByUKeyId(certFreezeReq.getUkeyId());
            if (null == certificateInfo) {
                throw new BusinessException("证书冻结失败,uKey信息不存在！", ErrorMessageConstant.ECUC0028_CODE);
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
            queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else{
            serviceRequestVO.setDn(certFreezeReq.getDn());
            queryWrapper.eq(CertificateManage::getDn, certFreezeReq.getDn());
        }

        // 发送请求
        cfcaCertService.certServiceRequest(serviceRequestVO);
//        certificateInfo.setCertStatus(SystemConstant.CERT_STATUS_5);
//        certificateInfo.setModifiedBy(certFreezeReq.getOperationUserId());
//        certificateInfo.setGmtModified(LocalDateTime.now());
//        boolean update = this.updateById(certificateInfo);
        boolean update = true;
        List<CertificateManage> lists = this.list(queryWrapper);
        for(int i=0;i<lists.size();i++) {
            lists.get(i).setCertStatus(SystemConstant.CERT_STATUS_5);
            lists.get(i).setModifiedBy(certFreezeReq.getOperationUserId());
            lists.get(i).setGmtModified(LocalDateTime.now());
            update = this.updateById(lists.get(i));
            if (!update) {
                throw new BusinessException("证书冻结失败！", ErrorMessageConstant.ECUC0028_CODE);
            }
        }
    }

    /**
     * 证书解冻
     * 
     * @param certThawReq
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void thawCert(CertThawReq certThawReq) throws Exception {
        // 验证用户是否存在
        /*if (verifyUserInformation(certThawReq.getUserId())) {
            throw new BusinessException("用户不存在，请重新输入！", "BE00001");
        }*/
        // 根据key号、id 查询证书DN
        LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
        CertServiceRequestVO serviceRequestVO = mapper.map(cfcaConfig, CertServiceRequestVO.class);
        CertificateManage certificateInfo = new CertificateManage();
        if(certThawReq.getUserId()!=null) {
            if(StringUtils.isNotBlank(certThawReq.getUkeyId())) {
                certificateInfo = queryUKeyInfo(certThawReq.getUserId(), certThawReq.getUkeyId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书解冻失败,uKey或用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }else{
                certificateInfo = queryUKeyInfoByUserId(certThawReq.getUserId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书解冻失败，用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
            queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else if(certThawReq.getUserId()==null&&StringUtils.isNotBlank(certThawReq.getUkeyId())){
            // 根据key号查询证书信息
            certificateInfo =
                    queryUKeyInfoByUKeyId(certThawReq.getUkeyId());
            if (null == certificateInfo) {
                throw new BusinessException("证书解冻失败,uKey信息不存在！", ErrorMessageConstant.ECUC0028_CODE);
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
            queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else{
            serviceRequestVO.setDn(certThawReq.getDn());
            queryWrapper.eq(CertificateManage::getDn, certThawReq.getDn());
        }
        // 创建CFCA请求参数对象
        serviceRequestVO.setTxCode(SystemConstant.TX_CODE_2601);
        // 发送请求
        cfcaCertService.certServiceRequest(serviceRequestVO);
//        certificateInfo.setCertStatus(SystemConstant.CERT_STATUS_4);
//        certificateInfo.setModifiedBy(certThawReq.getOperationUserId());
//        certificateInfo.setGmtModified(LocalDateTime.now());
//        boolean update = this.updateById(certificateInfo);
        boolean update = true;
        List<CertificateManage> lists = this.list(queryWrapper);
        for(int i=0;i<lists.size();i++) {
            lists.get(i).setCertStatus(SystemConstant.CERT_STATUS_4);
            lists.get(i).setModifiedBy(certThawReq.getOperationUserId());
            lists.get(i).setGmtModified(LocalDateTime.now());
            update = this.updateById(lists.get(i));
            if (!update) {
                throw new BusinessException("证书解冻失败！", ErrorMessageConstant.ECUC0028_CODE);
            }
        }
    }

    /**
     * 证书吊销
     *
     * @param certRevokeReq
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void revokeCert(CertRevokeReq certRevokeReq) throws Exception {
        // 验证用户是否存在
        /*if (verifyUserInformation(certRevokeReq.getUserId())) {
            throw new BusinessException("用户不存在，请重新输入！", "BE00001");
        }*/
        LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
        // 创建CFCA请求参数对象
        CertServiceRequestVO serviceRequestVO = mapper.map(cfcaConfig, CertServiceRequestVO.class);
        // 根据key号、id 查询证书DN
        CertificateManage certificateInfo = new CertificateManage();
        if(certRevokeReq.getUserId()!=null){
            if(StringUtils.isNotBlank(certRevokeReq.getUkeyId())) {
                certificateInfo = queryUKeyInfo(certRevokeReq.getUserId(), certRevokeReq.getUkeyId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书吊销失败，uKey或用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }else{
                certificateInfo = queryUKeyInfoByUserId(certRevokeReq.getUserId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书吊销失败，用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
            queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else if(certRevokeReq.getUserId()==null&&StringUtils.isNotBlank(certRevokeReq.getUkeyId())){
            // 根据key号查询证书信息
            certificateInfo =
                    queryUKeyInfoByUKeyId(certRevokeReq.getUkeyId());
            if (null == certificateInfo) {
                throw new BusinessException("证书吊销失败,uKey信息不存在！", ErrorMessageConstant.ECUC0028_CODE);
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
            queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else{
            serviceRequestVO.setDn(certRevokeReq.getDn());
            queryWrapper.eq(CertificateManage::getDn, certRevokeReq.getDn());
        }
        serviceRequestVO.setTxCode(SystemConstant.TX_CODE_2901);
        // 发送请求
        cfcaCertService.certServiceRequest(serviceRequestVO);
//        certificateInfo.setCertStatus(SystemConstant.CERT_STATUS_6);
//        certificateInfo.setModifiedBy(certRevokeReq.getOperationUserId());
//        certificateInfo.setGmtModified(LocalDateTime.now());
//        boolean update = this.updateById(certificateInfo);
        boolean update = true;
        List<CertificateManage> lists = this.list(queryWrapper);
        for(int i=0;i<lists.size();i++) {
            lists.get(i).setCertStatus(SystemConstant.CERT_STATUS_6);
            lists.get(i).setModifiedBy(certRevokeReq.getOperationUserId());
            lists.get(i).setGmtModified(LocalDateTime.now());
            update = this.updateById(lists.get(i));
            if (!update) {
                throw new BusinessException("证书吊销失败！", ErrorMessageConstant.ECUC0028_CODE);
            }
        }
    }

    /**
     * 证书更新
     * 
     * @param certUpdateReq
     * @return CertUpdateDto
     * @throws Exception
     */
    public CertUpdateDto updateCert(CertUpdateReq certUpdateReq) throws Exception {
        // 验证用户是否存在
        /*if (verifyUserInformation(certUpdateReq.getUserId())) {
            throw new BusinessException("用户不存在，请重新输入！", "BE00001");
        }*/
        // 根据key号、id 查询证书DN
        // 创建CFCA请求参数对象
        CertServiceRequestVO serviceRequestVO = mapper.map(cfcaConfig, CertServiceRequestVO.class);
        CertificateManage certificateInfo = new CertificateManage();
        if(certUpdateReq.getUserId()!=null) {
            if(StringUtils.isNotBlank(certUpdateReq.getUkeyId())) {
                certificateInfo = queryUKeyInfo(certUpdateReq.getUserId(), certUpdateReq.getUkeyId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书更新失败，uKey或用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }else{
                certificateInfo = queryUKeyInfoByUserId(certUpdateReq.getUserId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书更新失败，用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
        }else if(certUpdateReq.getUserId()==null&&StringUtils.isNotBlank(certUpdateReq.getUkeyId())){
            // 根据key号查询证书信息
            certificateInfo =
                    queryUKeyInfoByUKeyId(certUpdateReq.getUkeyId());
            if (null == certificateInfo) {
                throw new BusinessException("证书更新失败,uKey信息不存在！", ErrorMessageConstant.ECUC0028_CODE);
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
        }else{
            LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
            queryWrapper.eq(CertificateManage::getDn,certUpdateReq.getDn());
            certificateInfo = this.list(queryWrapper).get(0);
            serviceRequestVO.setDn(certUpdateReq.getDn());
        }

        serviceRequestVO.setTxCode(SystemConstant.TX_CODE_2701);
        serviceRequestVO.setDuration(certUpdateReq.getDuration());
        // 发送请求
        CertServiceResponseVO certServiceResponseVO = cfcaCertService.certServiceRequest(serviceRequestVO);
        // 更新信息入库
        certificateInfo.setGmtModified(LocalDateTime.now());
        certificateInfo.setModifiedBy(certUpdateReq.getOperationUserId());
        updateCertificateManage(certServiceResponseVO, certificateInfo);
        return mapper.map(certServiceResponseVO, CertUpdateDto.class);

    }

    /**
     * 证书下载
     * 
     * @param certDownloadReq
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public CertDownloadDto downloadCert(CertDownloadReq certDownloadReq) throws Exception {
        // 验证用户是否存在
        /*if (verifyUserInformation(certDownloadReq.getUserId())) {
            throw new BusinessException("用户不存在，请重新输入！", "BE00001");
        }*/
        // 创建CFCA请求参数对象
        CertServiceRequestVO serviceRequestVO = mapper.map(cfcaConfig, CertServiceRequestVO.class);
        LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
        // 根据key号、id 查询证书DN
        CertificateManage certificateInfo = new CertificateManage();
        if(certDownloadReq.getUserId()!=null) {
            if(StringUtils.isNotBlank(certDownloadReq.getUkeyId())) {
                certificateInfo = queryUKeyInfo(certDownloadReq.getUserId(), certDownloadReq.getUkeyId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书下载失败，uKey或用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }else{
                certificateInfo = queryUKeyInfoByUserId(certDownloadReq.getUserId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书下载失败，用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }
            serviceRequestVO.setSerialNo(certificateInfo.getSerialNo());
            serviceRequestVO.setAuthCode(certificateInfo.getAuthCode());
            queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else if(certDownloadReq.getUserId()==null&&StringUtils.isNotBlank(certDownloadReq.getUkeyId())){
            certificateInfo = queryUKeyInfoByUKeyId(certDownloadReq.getUkeyId());
            if (null == certificateInfo) {
                throw new BusinessException("证书下载失败，ukey不存在！", ErrorMessageConstant.ECUC0028_CODE);
            }
        serviceRequestVO.setSerialNo(certificateInfo.getSerialNo());
        serviceRequestVO.setAuthCode(certificateInfo.getAuthCode());
        queryWrapper.eq(CertificateManage::getDn, certificateInfo.getDn());
        }else{
            serviceRequestVO.setSerialNo(certDownloadReq.getSerialNo());
            serviceRequestVO.setAuthCode(certDownloadReq.getAuthCode());
            queryWrapper.eq(CertificateManage::getDn, certDownloadReq.getDn());
        }

        serviceRequestVO.setTxCode(SystemConstant.TX_CODE_2401);

        serviceRequestVO.setP10(certDownloadReq.getP10());
        // 发送请求
        CertServiceResponseVO certServiceResponseVO = cfcaCertService.certServiceRequest(serviceRequestVO);
        // 更新信息入库
//        certificateInfo.setSignatureCert(certServiceResponseVO.getSignatureCert());
//        certificateInfo.setCertStatus(SystemConstant.CERT_STATUS_4);
//        boolean update = this.updateById(certificateInfo);
//        if (update) {
//            return mapper.map(certServiceResponseVO, CertDownloadDto.class);
//        } else {
//            throw new BusinessException("证书下载失败！", ErrorMessageConstant.ECUC0028_CODE);
//        }
        boolean update = true;
        List<CertificateManage> lists = this.list(queryWrapper);
        for(int i=0;i<lists.size();i++) {
            lists.get(i).setCertStatus(SystemConstant.CERT_STATUS_4);
            lists.get(i).setModifiedBy(certDownloadReq.getOperationUserId());
            lists.get(i).setGmtModified(LocalDateTime.now());
            lists.get(i).setSignatureCert(certServiceResponseVO.getSignatureCert());
            update = this.updateById(lists.get(i));
            if (!update) {
                throw new BusinessException("证书下载失败！", ErrorMessageConstant.ECUC0028_CODE);
            }
        }
        return mapper.map(certServiceResponseVO, CertDownloadDto.class);
    }

    /**
     * 证书补发
     * 
     * @param certReissueReq
     * @throws Exception
     */
    public CertReissueDto reissueCert(CertReissueReq certReissueReq) throws Exception {
        // 验证用户是否存在
        /*if (verifyUserInformation(certReissueReq.getUserId())) {
            throw new BusinessException("用户不存在，请重新输入！", "BE00001");
        }*/
        // 创建CFCA请求参数对象
        CertServiceRequestVO serviceRequestVO = mapper.map(cfcaConfig, CertServiceRequestVO.class);
        // 根据key号、id 查询证书DN
        CertificateManage certificateInfo = new CertificateManage();
        if(certReissueReq.getUserId()!=null) {
            if(StringUtils.isNotBlank(certReissueReq.getUkeyId())) {
                certificateInfo = queryUKeyInfo(certReissueReq.getUserId(), certReissueReq.getUkeyId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书补发失败，uKey或用户不存在！", "");
                }
            }else{
                certificateInfo = queryUKeyInfoByUserId(certReissueReq.getUserId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书补发失败，用户不存在！", "");
                }
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
            certificateInfo.setDn(certificateInfo.getDn());
        }else if(certReissueReq.getUserId()==null&&StringUtils.isNotBlank(certReissueReq.getUkeyId())){
            certificateInfo = queryUKeyInfoByUKeyId(certReissueReq.getUkeyId());
            if (null == certificateInfo) {
                throw new BusinessException("证书补发失败，uKey不存在！", "");
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
            certificateInfo.setDn(certificateInfo.getDn());
        }else{
            serviceRequestVO.setDn(certReissueReq.getDn());
            LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
            queryWrapper.eq(CertificateManage::getDn,certReissueReq.getDn());
            certificateInfo = this.list(queryWrapper).get(0);
        }
//        LambdaQueryWrapper<CertificateManage> uKey = new LambdaQueryWrapper<>();
//        uKey.eq(CertificateManage::getUkeyId,certReissueReq.getNewUkeyId());
//        if(this.count(uKey) != 0){
//            throw new  BusinessException(ErrorMessageConstant.ECUC0032_MESSAGE,ErrorMessageConstant.ECUC0032_CODE);
//        }

        serviceRequestVO.setTxCode(SystemConstant.TX_CODE_2702);
        // 发送请求
        CertServiceResponseVO certServiceResponseVO = cfcaCertService.certServiceRequest(serviceRequestVO);
        // 更新信息入库

        certificateInfo.setUkeyId(certReissueReq.getNewUkeyId());
        certificateInfo.setModifiedBy(certReissueReq.getOperationUserId());
        updateCertificateManage(certServiceResponseVO, certificateInfo);
        return mapper.map(certServiceResponseVO, CertReissueDto.class);

    }

    /**
     * 证书换发
     * 
     * @param certRenewalReq
     * @throws Exception
     */
    public CertRenewalDto renewalCert(CertRenewalReq certRenewalReq) throws Exception {
        // 验证用户是否存在
        /*if (verifyUserInformation(certRenewalReq.getUserId())) {
            throw new BusinessException("用户不存在，请重新输入！", "BE00001");
        }*/
        // 创建CFCA请求参数对象
        CertServiceRequestVO serviceRequestVO = mapper.map(cfcaConfig, CertServiceRequestVO.class);
        // 根据key号、id 查询证书DN
        CertificateManage certificateInfo = new CertificateManage();
        if(certRenewalReq.getUserId()!=null){
            if(StringUtils.isNotBlank(certRenewalReq.getUkeyId())) {
                certificateInfo = queryUKeyInfo(certRenewalReq.getUserId(), certRenewalReq.getUkeyId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书换发失败，uKey或用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }else{
                certificateInfo = queryUKeyInfoByUserId(certRenewalReq.getUserId());
                if (null == certificateInfo) {
                    throw new BusinessException("证书换发失败，用户不存在！", ErrorMessageConstant.ECUC0028_CODE);
                }
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
        }else if(certRenewalReq.getUserId()==null&&StringUtils.isNotBlank(certRenewalReq.getUkeyId())){
            certificateInfo = queryUKeyInfoByUKeyId(certRenewalReq.getUkeyId());
            if (null == certificateInfo) {
                throw new BusinessException("证书换发失败，ukey不存在！", ErrorMessageConstant.ECUC0028_CODE);
            }
            serviceRequestVO.setDn(certificateInfo.getDn());
        }else{
            serviceRequestVO.setDn(certRenewalReq.getDn());
            LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
            queryWrapper.eq(CertificateManage::getDn,certRenewalReq.getDn());
            certificateInfo = this.list(queryWrapper).get(0);
        }
        serviceRequestVO.setTxCode(SystemConstant.TX_CODE_2703);
        serviceRequestVO.setDuration(certRenewalReq.getDuration());
        // 发送请求
        CertServiceResponseVO certServiceResponseVO = cfcaCertService.certServiceRequest(serviceRequestVO);
        // 更新信息入库
        certificateInfo.setModifiedBy(certRenewalReq.getOperationUserId());
        updateCertificateManage(certServiceResponseVO, certificateInfo);
        return mapper.map(certServiceResponseVO, CertRenewalDto.class);
    }

    /**
     * 证书查询
     * 
     * @param certQueryReq
     * @throws Exception
     */
    public List<CertQueryDto> queryCertInfo(CertQueryReq certQueryReq) throws Exception {
        List<CertQueryDto> certQueryDtos = new ArrayList<>();
        // 验证用户是否存在
        /*if (verifyUserInformation(certQueryReq.getUserId())) {
            throw new BusinessException("用户不存在，请重新输入！", "BE00001");
        }*/

        // 跨渠道访问交由内管控制 不由渠道端自行控制 by yangbh in 20200707 start
        // 验证渠道是否存在
//        if (selectCountChannel(certQueryReq.getChannel())){
//            throw new BusinessException(ErrorMessageConstant.ECUC0033_MESSAGE,ErrorMessageConstant.ECUC0033_CODE);
//        }

        // 跨渠道访问交由内管控制 不由渠道端自行控制 by yangbh in 20200707 end
        // 创建CFCA请求参数对象
        QueryRequestVO queryRequestVO = mapper.map(cfcaConfig, QueryRequestVO.class);
        // 根据key号、id 查询证书DN
        CertificateManage certificateInfo = new CertificateManage();
        if(certQueryReq.getUserId()!=null) {
            // 验证用户是否授权该渠道
            if (userAuthChannel(certQueryReq.getUserId(),certQueryReq.getChannel())){
                throw new BusinessException(ErrorMessageConstant.ECUC0034_MESSAGE,ErrorMessageConstant.ECUC0034_CODE);
            }
            if(StringUtils.isNotBlank(certQueryReq.getUkeyId())) {
                certificateInfo = queryUKeyInfo(certQueryReq.getUserId(), certQueryReq.getUkeyId());
                if (null == certificateInfo) {
                    throw new BusinessException(ErrorMessageConstant.ECUC0035_MESSAGE, ErrorMessageConstant.ECUC0035_CODE);
                }
            }else{
                certificateInfo = queryUKeyInfoByUserId(certQueryReq.getUserId());
                if (null == certificateInfo) {
                    throw new BusinessException(ErrorMessageConstant.ECUC0035_MESSAGE, ErrorMessageConstant.ECUC0035_CODE);
                }
            }
            queryRequestVO.setDn(certificateInfo.getDn());
        }else if(certQueryReq.getUserId()==null&&StringUtils.isNotBlank(certQueryReq.getUkeyId())){
            certificateInfo = queryUKeyInfoByUKeyId(certQueryReq.getUkeyId());
            if (null == certificateInfo) {
                throw new BusinessException(ErrorMessageConstant.ECUC0035_MESSAGE, ErrorMessageConstant.ECUC0035_CODE);
            }
            queryRequestVO.setDn(certificateInfo.getDn());
        }else{
            queryRequestVO.setDn(certQueryReq.getDn());
        }
        queryRequestVO.setTxCode(SystemConstant.TX_CODE_7101);
        // 发送请求,获取返结果集
        QueryResponseListVO queryResponseListVO = cfcaCertService.queryCertServiceRequest(queryRequestVO);
        List<QueryResponseVO> queryResponseVOList = queryResponseListVO.getQueryResponseVOList();
        if (queryResponseVOList != null && queryResponseVOList.size() > 0) {
            certQueryDtos =
                queryResponseVOList.stream().map(x -> mapper.map(x, CertQueryDto.class)).collect(Collectors.toList());
            return certQueryDtos;
        } else {
            throw new BusinessException(ErrorMessageConstant.ECUC0035_MESSAGE,ErrorMessageConstant.ECUC0035_CODE);
        }

    }

    /**
     * 验证用户信息是否存在
     *
     * @author Wangxz
     * @since 2019-11-05
     * @param userId
     * @return
     */
    /*public boolean verifyUserInformation(Integer userId) {
        userBaseInformationQueryWrapper = new QueryWrapper<>();
        userBaseInformationQueryWrapper.eq("user_id", userId);
        Integer selectCount = userBaseInformationMapper.selectCount(userBaseInformationQueryWrapper);
        return selectCount != 1;
    }*/

    /**
     * 查询用户信息
     *
     * @author Wangxz
     * @since 2019-11-05
     * @param userId
     * @return
     */
    public UserBaseInformation queryUserBaseInformation(Integer userId) {
        userBaseInformationQueryWrapper = new QueryWrapper<>();
        userBaseInformationQueryWrapper.eq("user_id", userId);
        // 排除港内员工的可能
        userBaseInformationQueryWrapper.ne("user_type",SystemConstant.USER_TYPE_1);
        // 排除自然人的可能
        userBaseInformationQueryWrapper.ne("user_type",SystemConstant.USER_TYPE_2);
        // 排除企业员工的可能
        userBaseInformationQueryWrapper.ne("user_type",SystemConstant.USER_TYPE_3);
        // 排除部门的可能
        userBaseInformationQueryWrapper.ne("user_type",SystemConstant.USER_TYPE_5);
        return userBaseInformationMapper.selectOne(userBaseInformationQueryWrapper);
    }

    /**
     * 查询用户证书信息
     *
     * @param userId
     * @param uKeyId
     * @return CertificateManage
     */
    public CertificateManage queryUKeyInfo(Integer userId, String uKeyId) {
        // 定义uKey信息表的queryWrapper对象
        QueryWrapper<CertificateManage> certificateManageQueryWrapper = new QueryWrapper<>();
        certificateManageQueryWrapper.eq("user_id", userId);
        certificateManageQueryWrapper.eq("ukey_id", uKeyId);
        certificateManageQueryWrapper.orderByDesc("gmt_modified");
        // 验证该用户下是否存在UKey
        if (this.count(certificateManageQueryWrapper) > 0) {
            return this.list(certificateManageQueryWrapper).get(0);
        } else {
            return null;
        }
    }

    public CertificateManage queryUKeyInfoByUKeyId(String uKeyId) {
        // 定义uKey信息表的queryWrapper对象
        QueryWrapper<CertificateManage> certificateManageQueryWrapper = new QueryWrapper<>();
        certificateManageQueryWrapper.eq("ukey_id", uKeyId);
        certificateManageQueryWrapper.orderByDesc("gmt_modified");
        // 验证该用户下是否存在UKey
        if (this.count(certificateManageQueryWrapper) > 0) {
            return this.list(certificateManageQueryWrapper).get(0);
        } else {
            return null;
        }
    }
    public CertificateManage queryUKeyInfoByUserId(Integer userId) {
        // 定义uKey信息表的queryWrapper对象
        QueryWrapper<CertificateManage> certificateManageQueryWrapper = new QueryWrapper<>();
        certificateManageQueryWrapper.eq("user_id", userId);
        certificateManageQueryWrapper.orderByDesc("gmt_modified");
        // 验证该用户下是否存在UKey
        if (this.count(certificateManageQueryWrapper) > 0) {
            return this.list(certificateManageQueryWrapper).get(0);
        } else {
            return null;
        }
    }

    /**
     * 更新证书信息
     *
     * @param certServiceResponseVO
     * @param certificateManage
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCertificateManage(CertServiceResponseVO certServiceResponseVO,
        CertificateManage certificateManage) throws BusinessException {
//        certificateManage.setSerialNo(certServiceResponseVO.getSerialNo());
//        certificateManage.setAuthCode(certServiceResponseVO.getAuthCode());
//        certificateManage.setStartTime(certServiceResponseVO.getStartTime());
//        certificateManage.setEndTime(certServiceResponseVO.getEndTime());
//        certificateManage.setCertStatus(SystemConstant.CERT_STATUS_3);
//        certificateManage.setGmtModified(LocalDateTime.now());
//        if (this.updateById(certificateManage)) {
//            return true;
//        } else {
//            throw new BusinessException("证书信息更新失败", ErrorMessageConstant.ECUC0028_CODE);
//        }
        boolean update = true;
        LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
        queryWrapper.eq(CertificateManage::getDn, certificateManage.getDn());
        List<CertificateManage> lists = this.list(queryWrapper);
        for(int i=0;i<lists.size();i++) {
            lists.get(i).setSerialNo(certServiceResponseVO.getSerialNo());
            lists.get(i).setAuthCode(certServiceResponseVO.getAuthCode());
            lists.get(i).setStartTime(certServiceResponseVO.getStartTime());
            lists.get(i).setEndTime(certServiceResponseVO.getEndTime());
            lists.get(i).setCertStatus(SystemConstant.CERT_STATUS_3);
            lists.get(i).setGmtModified(LocalDateTime.now());
            lists.get(i).setModifiedBy(certificateManage.getModifiedBy());
            lists.get(i).setUkeyId(certificateManage.getUkeyId());
            update = this.updateById(lists.get(i));
            if (!update) {
                throw new BusinessException("证书信息更新失败", ErrorMessageConstant.ECUC0028_CODE);
            }
        }
        return true;

    }

    /**
     * 证件类型转换
     *
     * @param papersType
     * @return
     */
    public String queryPapersType(String papersType) {
        if (null == papersType){
            throw new BusinessException("用户证件类型不能为空", ErrorMessageConstant.ECUC0028_CODE);
        }
        switch (papersType) {
            // 统一社会信用代码
            case SystemConstant.PAPERS_TYPE_00:
                return SystemConstant.CFCA_PAPERS_TYPE_N;
            // 组织机构代码证
            case SystemConstant.PAPERS_TYPE_01:
                return SystemConstant.CFCA_PAPERS_TYPE_7;
            // 营业执照
            case SystemConstant.PAPERS_TYPE_02:
                return SystemConstant.CFCA_PAPERS_TYPE_8;
            // 税务登记证
            case SystemConstant.PAPERS_TYPE_03:
                return SystemConstant.CFCA_PAPERS_TYPE_4;
            // 身份证
            case SystemConstant.PAPERS_TYPE_04:
                return SystemConstant.CFCA_PAPERS_TYPE_0;
            // 其他
            default:
                return SystemConstant.CFCA_PAPERS_TYPE_Z;
        }
    }

    // 跨渠道访问交由内管控制 不由渠道端自行控制 by yangbh in 20200707 start
/*
    */
/**
     * 验证渠道是否存在
     *
     * @param channel
     * @return
     *//*

    public boolean selectCountChannel(String channel){
        QueryWrapper<SysDataDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("data_type_no","channel");
        queryWrapper.eq("data_no",channel);
        Integer integer = sysDataDicMapper.selectCount(queryWrapper);
        return integer == 0;
    }
*/

    // 跨渠道访问交由内管控制 不由渠道端自行控制 by yangbh in 20200707 end
    /**
     * 验证用户是否授权该渠道
     *
     * @param userId
     * @param channel
     * @return
     */
    public boolean userAuthChannel(Integer userId, String channel){
//        QueryWrapper<UserBaseInformation> queryWrapper = new QueryWrapper<>();
//        String like = "%"+channel+"%";
//        queryWrapper.apply("(authorized_channels like {0} or channel = {1})",like,channel);
//        queryWrapper.eq("user_id", userId);
//        Integer integer = userBaseInformationMapper.selectCount(queryWrapper);
        Integer integer = 0;
        UserBaseInformation userBaseInformation = userBaseInformationMapper.selectById(userId);
        if(userBaseInformation!=null) {
            if(SystemConstant.USER_TYPE_0.equals(userBaseInformation.getUserType())) {
                QueryWrapper<EnterpriseBasicInfo> queryWrapper = new QueryWrapper<>();
                String like = "%"+channel+"%";
                queryWrapper.apply("(authorized_channels like {0} or channel = {1})",like,channel);
                queryWrapper.eq("user_id", userId);
                integer = enterpriseBasicInfoMapper.selectCount(queryWrapper);
            }else if(SystemConstant.USER_TYPE_4.equals(userBaseInformation.getUserType())) {
                LambdaQueryWrapper<OrganizationalStructure> queryWrapperOS = new LambdaQueryWrapper<OrganizationalStructure>();
                queryWrapperOS.eq(OrganizationalStructure::getOrganizationalId, userId);
                integer = organizationalStructureMapper.selectCount(queryWrapperOS);
            }
        }
        return integer == 0;
    }

    /**
     * 用户绑定cfca证书信息
     * @param
     * @return
     */
    public String bindingCert(BindingCertReq req) {
        UserBaseInformation userBaseInformation = queryUserBaseInformation(req.getUserId());
      // 验证用户是否存在，组织和企业
      if (null == userBaseInformation) {
          throw new BusinessException(ErrorMessageConstant.ECUC0002_MESSAGE, ErrorMessageConstant.ECUC0002_CODE);
      }
        QueryWrapper<CertificateManage> uKey = new QueryWrapper<>();
        // 验证是否已申请该uKey
        if (null != queryUKeyInfo(req.getUserId(), req.getUkeyId())){
            throw new BusinessException("用户已申请该uKey，请勿重复申请", ErrorMessageConstant.ECUC0028_CODE);
        }
        LambdaQueryWrapper<CertificateManage> queryWrapper = new LambdaQueryWrapper<CertificateManage>();
        queryWrapper.eq(CertificateManage::getUkeyId, req.getUkeyId());
        queryWrapper.orderByDesc(CertificateManage::getGmtModified);
        List<CertificateManage> lists = this.list(queryWrapper);
        if(lists.size()>0) {
            CertificateManage certificateManage = lists.get(0);
            certificateManage.setUserId(req.getUserId());
            this.save(certificateManage);
        }else {
            throw new BusinessException("该ukey未绑定证书信息", ErrorMessageConstant.ECUC0028_CODE);
        }
        return SystemConstant.SYSTEM_SUCCESS;
    }
}
